package com.scap.authen.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.context.ApplicationContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.servlet.support.RequestContextUtils;

import com.scap.authen.dao.SiteDao;
import com.scap.authen.entity.Site;
import com.scap.authen.util.JDate;

@Service("siteService")
@Transactional
public class SiteServiceImpl implements SiteService{

	
	@Autowired
	private SiteDao voSiteDao;
	@Autowired
	private ApplicationContext voContext;
	
	@Override
	public List<Object> findAllSite() {
		
		return voSiteDao.getAllSite();
	}

	@Override
	public List<Object> getListBasedOnSearchParameter(String searchParameter,List<Object> siteList) {
		
		if (null != searchParameter && !searchParameter.equals("")) {
			List<Object> siteForSearch = new ArrayList<Object>();
			searchParameter = searchParameter.toUpperCase();
			for (int i=0; i<siteList.size(); i++) {
				
				Site site = (Site) siteList.get(i);
				if (site.getVaSiteId().toUpperCase().indexOf(searchParameter)!= -1 || site.getVaSiteName().toUpperCase().indexOf(searchParameter)!= -1
						|| site.getVaStatus().toUpperCase().indexOf(searchParameter)!= -1) {
					siteForSearch.add(site);					
				}
				
			}
			siteList = siteForSearch;
			siteForSearch = null;
		}
		return siteList;
	}

	@Override
	public String saveSite(Site poSite,HttpServletRequest request) {
		
		String vaResult = "";
		Locale voLocale = RequestContextUtils.getLocale(request);
		poSite.setVnInsertUserId(1);
		poSite.setVaInsertDate(JDate.getDate());
		poSite.setVaInsertTime(JDate.getTime());
		
		if ( voSiteDao.insertSite(poSite)){
			vaResult = voContext.getMessage("alert.save.success", null, voLocale);
		} else {
			vaResult = voContext.getMessage("alert.save.fail", null, voLocale);
		}
		
		return vaResult;
	}

	@Override
	public String updateSite(Site poSite, HttpServletRequest request) {
		String vaResult = "";
		
		Locale voLocale = RequestContextUtils.getLocale(request);
			 
		poSite.setVnUpdateUserId(1);
		poSite.setVaUpdateDate(JDate.getDate());
		poSite.setVaUpdateTime(JDate.getTime());
		
		if ( voSiteDao.updateSite(poSite)){
			vaResult = voContext.getMessage("alert.save.success", null, voLocale);
		} else {
			vaResult = voContext.getMessage("alert.save.fail", null, voLocale);
		}	
		return vaResult;
	}
	
	
	@Override
	public List<HashMap<String, String>> getAllSiteAndRole() {
		
		List<Object[]> voSiteAndRoleList = voSiteDao.getAllSiteAndRole();
		
		List<HashMap<String, String>> voSiteAndRoleListHashMap = new ArrayList<HashMap<String,String>>();
		
		for(Object[] voObject : voSiteAndRoleList){
			HashMap<String, String> voHash = new HashMap<String , String>();

			voHash.put("vaSiteId", String.valueOf(voObject[0]));
			voHash.put("vaSiteName", String.valueOf(voObject[1]));
			voHash.put("vaRoleId", String.valueOf(voObject[2]));
			voHash.put("vaRoleName", String.valueOf(voObject[3]));
			voSiteAndRoleListHashMap.add(voHash);
		}
		
		return voSiteAndRoleListHashMap;
	}

	@Override
	public Map<String, String> getSiteList() {
		Map<String,String> voSite = new LinkedHashMap<String,String>();
		
		List<Object[]> voSiteList = voSiteDao.getSiteList();
		
		for(Object[] voObject : voSiteList){

			voSite.put(String.valueOf(voObject[0]), String.valueOf(voObject[1]));

		}
		return voSite;
	}
}
